package com.chixing.controller;

import com.chixing.entity.Hall;
import com.chixing.service.IHallService;
import com.chixing.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 影厅前端控制器
 * </p>
 *
 * @author liwenjing
 * @since 2025-10-07
 */
@RestController
@RequestMapping("/hall")
public class HallController {

    @Autowired
    private IHallService hallService;

    /**
     * 获取所有影厅
     * @return 影厅列表
     */
    @GetMapping("")
    public Result getAllHalls() {
        try {
            List<Hall> halls = hallService.list();
            return Result.getSuccess(halls);
        } catch (Exception e) {
            return Result.error("获取影厅列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取影厅详情
     * @param hallId 影厅ID
     * @return 影厅信息
     */
    @GetMapping("/{hallId}")
    public Result getHall(@PathVariable Integer hallId) {
        try {
            Hall hall = hallService.getById(hallId);
            if (hall != null) {
                return Result.getSuccess(hall);
            } else {
                return Result.error("未找到指定的影厅");
            }
        } catch (Exception e) {
            return Result.error("获取影厅详情失败: " + e.getMessage());
        }
    }

    /**
     * 添加影厅
     * @param hall 影厅对象
     * @return 添加结果
     */
    @PostMapping("")
    public Result addHall(@RequestBody Hall hall) {
        try {
            hall.setCreateTime(LocalDateTime.now());
            hall.setUpdateTime(LocalDateTime.now());
            boolean result = hallService.save(hall);
            if (result) {
                return Result.saveSuccess("添加影厅成功");
            } else {
                return Result.error("添加影厅失败");
            }
        } catch (Exception e) {
            return Result.error("添加影厅异常: " + e.getMessage());
        }
    }

    /**
     * 更新影厅
     * @param hallId 影厅ID
     * @param hall 影厅对象
     * @return 更新结果
     */
    @PutMapping("/{hallId}")
    public Result updateHall(@PathVariable Integer hallId, @RequestBody Hall hall) {
        try {
            hall.setHallId(hallId);
            hall.setUpdateTime(LocalDateTime.now());
            boolean result = hallService.updateById(hall);
            if (result) {
                return Result.updateSuccess("更新影厅成功");
            } else {
                return Result.error("更新影厅失败");
            }
        } catch (Exception e) {
            return Result.error("更新影厅异常: " + e.getMessage());
        }
    }

    /**
     * 删除影厅
     * @param hallId 影厅ID
     * @return 删除结果
     */
    @DeleteMapping("/{hallId}")
    public Result deleteHall(@PathVariable Integer hallId) {
        try {
            boolean result = hallService.removeById(hallId);
            if (result) {
                return Result.deleteSuccess("删除影厅成功");
            } else {
                return Result.error("删除影厅失败");
            }
        } catch (Exception e) {
            return Result.error("删除影厅异常: " + e.getMessage());
        }
    }
}

